package com.jacen.fzxs.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.jacen.fzxs.entity.RightEntity;
import com.jacen.fzxs.service.IRightService;
import com.jacen.fzxs.util.DateUtils;
import com.jacen.fzxs.util.ServletUtils;
import com.jacen.fzxs.util.StrUtils;
import com.jacen.fzxs.util.TreeNode;
import com.jacen.fzxs.util.Utils;

/**
 * 权限控制
 * @author Jacen
 *
 */
@Controller
public class RightController extends BaseController {
	
	@Resource
	private IRightService rightService;
	private static final Logger logger = LoggerFactory.getLogger(RightController.class);
	
	@RequestMapping("/right/list.do")
	public String list(HttpServletRequest request,String pid,ModelMap modelMap,Integer curPage) throws Exception{
		Map<String, String> params = ServletUtils.getParameterMap(request);
		List<RightEntity> list = rightService.getPage(getPager(curPage,request), params);
		modelMap.addAttribute("pid", pid);
		modelMap.addAttribute("list", list);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"->用户："+getLocalUser(request).getUserName()+"查询权限列表："+list.size());
		return "right/list";
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/right/insert.do")
	public String enterInsert(String pid,ModelMap map){
		map.addAttribute("pid", pid);
		return "right/insert";
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/right/insert.do")
	@ResponseBody
	public String postInsert(RightEntity right,HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			rightService.insert(right);
			resultMap.put("code", 1);
			resultMap.put("pid", right.getUpRightId());
			logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"->用户："+getLocalUser(request).getUserName()+"新增权限："+right.getRightName());
		} catch (Exception e) {
			resultMap.put("code", 0);
			resultMap.put("message", e.getMessage());
			resultMap.put("pid", right.getUpRightId());
		}
		return JSON.toJSONString(resultMap);
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/right/update.do")
	public String enterUpdate(String id,ModelMap modelMap){
		RightEntity entity = rightService.getById(id);
		modelMap.addAttribute("right", entity);
		return "right/update";
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/right/update.do")
	@ResponseBody
	public String postUpdate(RightEntity entity,HttpServletRequest request){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try{
			rightService.update(entity);
			resultMap.put("code", 1);
			resultMap.put("pid", entity.getUpRightId());
			logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"->用户："+getLocalUser(request).getUserName()+"更新权限："+entity.getRightName());
			return JSON.toJSONString(resultMap);
		}catch(Exception e){
			return Utils.errorJsonResp(e.getMessage());
		}
		
	}
	
	@RequestMapping(method=RequestMethod.GET,value="/right/view.do")
	public String view(ModelMap map,String id){
		RightEntity entity = rightService.getById(id);
		map.put("right", entity);
		return "right/view";
	}
	
	@RequestMapping(method=RequestMethod.POST,value="/right/delete.do")
	@ResponseBody
	public String delete(ModelMap map,String id,String pid,HttpServletRequest request){
		rightService.deleteAndChilds(id);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code", 1);
		resultMap.put("pid", pid);
		logger.info(DateUtils.getNowFormatTime("yyyy-MM-dd hh:mm:ss")+"->用户："+getLocalUser(request).getUserName()+"删除权限："+id);
		return JSON.toJSONString(resultMap);
	}
	
	@RequestMapping(value = "/right/rightframe.do", method = RequestMethod.GET)
	public String roleright(HttpServletRequest request, String pid,ModelMap modelMap) throws Exception {
		if(StrUtils.isEmpty(pid)){
			pid = "1";
		}
		modelMap.addAttribute("pid", pid);
		return "/right/rightframe";
	}
	
	@RequestMapping("/right/rightTree.do")
	public String tree(HttpServletRequest request,ModelMap modelMap){
		List<RightEntity> list = rightService.getAll();
		modelMap.addAttribute("list", toTreeBeanListJson(list));
		return "right/righttree";
	}
	
	private String toTreeBeanListJson(List<RightEntity> list){
		List<TreeNode> treeBeans = new ArrayList<TreeNode>();
		TreeNode bean = new TreeNode();
		bean.setId("1");
		bean.setName("系统权限");
		bean.setOpen(true);
		bean.setUrl("right/list.do?pid=1");
		bean.setTarget("rightList");
		treeBeans.add(bean);
		for(RightEntity entity : list){
			bean = new TreeNode();
			bean.setId(entity.getId());
			bean.setPId(entity.getUpRightId());
			bean.setName(entity.getRightName());
			bean.setUrl("right/list.do?pid="+entity.getId());
			bean.setTarget("rightList");
			//bean.setChecked(true);
			bean.setOpen((entity.getOpen()==null||entity.getOpen()==0)?false:true);
			treeBeans.add(bean);
		}
		return JSON.toJSONString(treeBeans);
	}

}
